<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/31
 * Time: 下午9:09
 */

namespace Home\Controller;
use Think\Controller;

class SelfController extends Controller{

    //个人页面
    public function index(){
        if(isset($_SESSION['loginUser'])) {
            //获取有无未读消息
            $message_db = D("Message");
            $message_result = $message_db->getIsRead($_SESSION['loginUser']['user_id']);
            if (is_array($message_result)) {
                session('is_read', 1);
            } else {
                session('is_read', 2);
            }
        }
        $this->assign('curNav', 3);
        $this->display();
    }

    //个人页面信息获取
    public function selfInfo(){
        if(isset($_SESSION['loginUser'])){
            if(IS_GET){
                $user_id = $_SESSION['loginUser']['user_id'];
                $user_ext_db = D('UserExt');
                $user_ext_result = $user_ext_db->getSelfInfo($user_id);
                if($user_ext_result){
                    $this->success('获取信息成功',$user_ext_result);
                }else{
                    $this->error('获取信息失败');
                }
            }
        }else{
            $this->error('请登录系统',U('Login/index'));
        }
    }

    //pc端收藏的用户
    public function indexUserPC(){
        $this->theme('PC')->display('Message/user');
    }

    //收藏页面跳转
    public function favorite(){
        if(isset($_SESSION['loginUser'])){
            $this->assign('curNav', 3);
            $this->display();
        }else{
            $this->display('Login/index');
        }
    }

    //隐身状态页面跳转
    public function cloak(){
        if(isset($_SESSION['loginUser'])){
            $this->assign('curNav', 3);
            $this->display();
        }else{
            $this->display('Login/index');
        }
    }

    //等级说明页面跳转
    public function rank(){
        $this->assign('curNav', 3);
        $this->display();
    }

    public function rankPC(){
        $this->theme('PC')->display('Rank/rank');
    }


    //关于我们页面跳转
    public function aboutus(){
        $this->assign('curNav', 3);
        $this->display();
    }

    //收藏页面－用户
    public function collectUser(){
        if(isset($_SESSION['loginUser'])){
            if(IS_GET){
                $user_id = $_SESSION['loginUser']['user_id'];
                $collect_db = D('Collect');
                $collect_result = $collect_db->getCollectUser($user_id);
                if($collect_result){
                    $this->success('获取成功', $collect_result);
                }else{
                    $this->error('获取失败');
                }
            }
        }else{
            $this->error('请登录系统',U('Login/index'));
        }
    }

    //收藏页面－任务
    public function collectMission(){
        if(isset($_SESSION['loginUser'])){
            if(IS_GET){
                $user_id = $_SESSION['loginUser']['user_id'];
                $collect_db = D('Collect');
                $collect_result = $collect_db->getCollectMission($user_id);
                if($collect_result){
                    $this->success('获取成功', $collect_result);
                }else{
                    $this->error('获取失败');
                }
            }
        }else{
            $this->error('请登录系统',U('Login/index'));
        }
    }

    public function toPublisher(){
        //获取有无未读消息
        $message_db = D("Message");
        $message_result = $message_db->getIsRead($_SESSION['loginUser']['user_id']);
        if(is_array($message_result)){
            session('is_read',1);
        }else{
            session('is_read',2);
        }
        session('entrance', 1);
        $this->assign('curNav', 0);
        $this->display('Publisher/index');
    }

    public function toFetcher(){
        session('entrance', 0);
        $this->assign('curNav', 0);
        if(isset($_SESSION['loginUser'])){
            $this->display("Fetcher/search");
        } else {
            $this->display("Fetcher/index");
        }
    }

    //隐身状态pc
    public function indexPC(){
        if(isset($_SESSION['loginUser'])) {
            $this->assign('curNav', 3);
            $this->theme('PC')->display('Status/index');
        } else {
            $this->display('Login/loginPC');
        }
    }

    //启动隐身状态
    public function inVisible(){
        if(IS_GET){
            $user_id = $_SESSION['loginUser']['user_id'];
            $data['status'] = 3;
            $user_db = M('User');
            $user_result = $user_db->where(array('user_id'=>$user_id))->save($data);
            if($user_result){
                $user_db = D('User');
                $user_info = $user_db->getByUser_id($_SESSION['loginUser']['user_id']);
                session('loginUser',$user_info);
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //关闭隐身状态
    public function visible(){
        if(IS_GET){
            $user_id = $_SESSION['loginUser']['user_id'];
            $data['status'] = 1;
            $user_db = M('User');
            $user_result = $user_db->where(array('user_id'=>$user_id))->save($data);
            if($user_result){
                $user_db = D('User');
                $user_info = $user_db->getByUser_id($_SESSION['loginUser']['user_id']);
                session('loginUser',$user_info);
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //test
    public function test(){
        $user_id = $_GET['user_id'];
        $collect_db = D('Collect');
        $collect_result = $collect_db->getCollectMission($user_id);
        dump($collect_result);
    }

} 